package com.server.common.utils;

import com.google.gson.Gson;
import com.server.common.CustomException.CustomExcption;
import com.server.common.config.Global;
import com.server.modules.api.entity.dayu.Results;
import com.server.modules.sys.entity.tels.TMsgSend;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * Created by 金贤哲 on 2017/3/15 0015.
 */
public class SendSMS {
    public static final String URL = Global.getConfig("alidayu.url");
    public static final String APPKEY = Global.getConfig("alidayu.appKey");
    public static final String SECRET = Global.getConfig("alidayu.secret");

    @Resource
    private TMsgSend tMsgSend;

    public String listToString(List list, char separator) {
        StringBuilder sd = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sd.append(list.get(i)).append(separator);
        }
        return sd.toString().substring(0,sd.toString().length()-1);
    }

    public Results sendSms(String msg,List<String> tels,String temName) throws CustomExcption {
        if (msg ==null || msg.length() == 0){
           return null;
        }
        if (tels == null ||tels.size() == 0){
                 return null;
        }
        TaobaoClient client = new DefaultTaobaoClient(URL, APPKEY, SECRET);
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend("123456");
        req.setSmsType("normal");
        req.setSmsFreeSignName("贷贷帮"); //短信签名名称

        req.setSmsParamString("{'code':'" + getSendMsgByTemName(msg,temName) + "','product':'贷贷帮'}");
        String telephone = listToString(tels,',');
        System.out.println(telephone);

        req.setRecNum(telephone);
        req.setSmsTemplateCode(temName);
        try {
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);

            Results res = new Gson().fromJson(rsp.getBody(), Results.class);
            System.out.println(res.toString());
            return res;
        } catch (ApiException e) {
            e.printStackTrace();
            return null;
        }
    }

    public String getSendMsgByTemName(String msg,String temName){
        //去数据库里面找短信模板
        if(msg == tMsgSend.getMsgname()){
            temName = tMsgSend.getRecipient();
        }
        //拼接字符串
        return temName;
    }

    public boolean isPhoneNum(String phoneNum){
        return  isChinaPhoneLegal(phoneNum);
    }


    private boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
        String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

}
